/**
 * Created by lwy on 2017/8/1.
 */


$('.layer-date').datetimepicker({
    language:  'zh-CN',
    minView: 'month',
    format: 'yyyy-mm-dd HH:mm:ss',
    autoclose: true,
    startView: 2,
    showMeridian: 1,
    pickerPosition: "bottom-left"
});
var page = 1;
var id;
var label,
    name="",
    activityState="";
function doAjax(){
    $.ajax({
        url:'/crm/activityC/search.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNu:page,
            label:label,
            name:name,
            activityState:activityState,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.length;i++){
                    var delHtml = '';
                    var alterHtml = '';
                    var delStyle = '';
                    if(data.datas[i].activityStateValue==1){
                        delHtml='无效';
                        alterHtml='修改';
                    }else if(data.datas[i].activityStateValue==0){
                        delHtml='有效';
                        alterHtml='';
                    }


                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;" id="'+data.datas[i].id+'">'+data.datas[i].id+'</td></a>'+
                        '<td class=" ">'+data.datas[i].name+'</td>'+
                        '<td class=" ">'+data.datas[i].label+'</td>'+
                        '<td class="center ">'+data.datas[i].activityState+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].startTime+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].endTime+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].createTime+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].remark+'</td>'+
                        '<td class="sorting_1 delId" style="text-decoration: underline;cursor: pointer;'+delStyle+'" delid="'+data.datas[i].id+'">'+delHtml+'</td>'+
                        '<td class="sorting_1 alterId" style="text-decoration: underline;cursor: pointer;'+delStyle+'" id="'+data.datas[i].id+'"  data-toggle="modal" data-target="#myModal4">'+alterHtml+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }



                //无效/有效角色
                $('.delId').unbind('click').click(function(){
                    var recoverHtml = $(this).html();
                    var url = '';
                    if(recoverHtml=='无效'){
                        url='/crm/activityC/invalid.json';
                    }else if(recoverHtml=='有效'){
                        url='/crm/activityC/effective.json';
                    }
                    if(confirm("你确定要"+recoverHtml+"该活动？")){
                        $.ajax({
                            url:url,
                            dataType:'json',
                            data:{
                                token:localStorage.getItem('crmtoken'),
                                id:$(this).attr('delid'),
                            },
                            type:'post',
                            success:function(data){
                                if(data.code==-1){
                                    window.parent.location.href="/crm/login.jsp"
                                }
                                if(data.code==0){
                                    alert(data.msg)
                                    return;
                                }
                                if(data.code==1){
                                    alert(''+recoverHtml+'成功');
                                    page=1;
                                    doAjax();
                                }
                            }
                        })

                    }
                })

                //修改
                $('.alterId').unbind('click').click(function(){
                    id = $(this).attr('id');

                    $.ajax({
                        url:'/crm/activityC/detail.json',
                        dataType:'json',
                        data:{
                            id:id,
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==1){
                                $('#name2').val(data.datas.name);
                                $('#label2').val(data.datas.label);
                                $('#datetimepicker').val(data.datas.startTime);
                                $('#datetimepicker2').val(data.datas.endTime);
                                $('#remark2').val(data.datas.remark);
                                $('#state2').val(data.datas.activityState);
                            }
                        }
                    })
                })
                $('#save2').unbind('click').click(function(){
                    $.ajax({
                        url:'/crm/activityC/edit.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            id:id,
                            startTime:$('#datetimepicker').val(),
                            endTime:$('#datetimepicker2').val(),
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==1){
                                alert('修改成功');
                                $('#myModal4').removeClass('fade in');
                                $('#myModal4').addClass('fade');
                                $('#myModal4').attr('aria-hidden',true);
                                $('#myModal4').css('display','none');
                                $('.modal-backdrop').css('display','none');
                                page=1;
                                doAjax();
                            }
                        }
                    })
                })

                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    pageCount:data.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}




$(function(){
    doAjax();
})

//查询
$('#refer').click(function(){
   lable = $('#lable').val();
    name = $('#name').val();
    activityState = $('#activityState').val();
    page=1;
    doAjax();
})






